package util.cleaner;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

import util.io.FileInput;

public class TrimDeliciousGraph {

	static private int _id_url = 0;
	static private int _id_tag = 1;
	static private int _url = 2;
	static private int _tag = 3;
	static private int _freq_u = 5;
	static private int _freq_tag = 4;
	static private int _freq_ut = 6;
	static private int _age = 7;
	
	int offset= 10000000;
	
	public static void trimByDomain(String path) throws MalformedURLException{
		
		FileInput in = new FileInput(path);
		String line= in.readString();
		
		
		
	
		line= in.readString();
	
		while(line!=null){

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			url_name = url_name.replace("www.", "");
			
			
			if(!(name.matches("[a-z]") && name.length()==1)){
				
				String domain  = getDomain(url_name).trim();
				
				System.out.println(id_url + "\t"+ id_tag + "\t"+ domain + "\t"+ name +"\t"
						+freq_tag + "\t"+ freq_url + "\t"+ freq_ut + "\t"+ age);
				
				
			}
		
			line=in.readString();
			
			
		}
		
		
		
	}
	
	
	public static String getDomain(String url) throws MalformedURLException{
		
		
		URL u = new URL(url);
		
		return u.getHost();
		
	}
	public static void main(String[] args) throws MalformedURLException {
		// TODO Auto-generated method stub
		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz.txt";
		
		trimByDomain(path);

	}

}
